package com.spica.spicaschool.view

import android.content.Context
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import androidx.core.view.marginTop
import coil.load
import com.spica.spicaschool.R
import com.spica.spicaschool.base.CompatViewGroup

@Suppress("MemberVisibilityCanBePrivate", "unused")
class ItemDiary : CompatViewGroup {

  constructor(context: Context?) : super(context) {
    initStyle()
  }

  constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
    initStyle()
  }


  private fun initStyle() {
    val lp = LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT
    )

    lp.setMargins(12.dp, 0, 12.dp, 0)

    layoutParams = lp
    setPadding(16.dp, 8.dp, 16.dp, 8.dp)

    background = context.getDrawable(R.drawable.bg_md_card)
  }

  //头像
  val avatar = ImageView(context).apply {
    val lp = LayoutParams(
      40.dp,
      40.dp
    )
    layoutParams = lp
    load(R.drawable.default_head)
    this@ItemDiary.addView(this)
  }

  //用户名
  val author = TextView(context).apply {
    val lp = LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT,
      ViewGroup.LayoutParams.WRAP_CONTENT
    )
    layoutParams = lp
    text = "名称"
    this@ItemDiary.addView(this)
  }

  //发布类型
  val pushType = TextView(context).apply {
    val lp = LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT,
      ViewGroup.LayoutParams.WRAP_CONTENT
    )
    layoutParams = lp
    text = "发布了动态"
    this@ItemDiary.addView(this)
  }

  //发布时间
  val timeView = TextView(context).apply {
    val lp = LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT
    )
    layoutParams = lp
    text = "2020:01:02"
    this@ItemDiary.addView(this)
  }

  //标题
  val title = TextView(context).apply {
    val lp = LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT
    )
    layoutParams = lp
    text = "标题"
    this@ItemDiary.addView(this)
  }

  //内容
  val info = TextView(context).apply {
    val lp = LayoutParams(
      ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.WRAP_CONTENT
    )
    lp.setMargins(0, 12.dp, 0, 0)
    layoutParams = lp
    text =
      "3月23日，在天能集团沭阳公司生产车间里，机器人手臂在流水线上操作，从铅块到成品再到废料回收，实现循环再利用的智能化生产。据悉，天能集团从2005年到沭阳投资以来，已累计投资25亿元。主要生产纳米高能蓄电池、牵引用蓄电池等新能源产品，目前已成长为国内最大的电动车用纳米高能蓄电池生产基地。"
    this@ItemDiary.addView(this)
  }

  //点赞按钮
  val btnLike = ImageView(context).apply {
    val lp = LayoutParams(
      24.dp,
      24.dp
    )
    lp.setMargins(12.dp, 12.dp, 12.dp, 12.dp)
    layoutParams = lp
    load(R.drawable.ic_like_normal)
    this@ItemDiary.addView(this)
  }

  //点赞人数
  val likeNum = TextView(context).apply {
    val lp = LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT,
      ViewGroup.LayoutParams.WRAP_CONTENT
    )
    lp.setMargins(12.dp, 12.dp, 12.dp, 12.dp)
    layoutParams = lp
    text = "0"
    this@ItemDiary.addView(this)
  }

  //评论按钮
  val btnReply = ImageView(context).apply {
    val lp = LayoutParams(
      24.dp,
      24.dp
    )
    lp.setMargins(12.dp, 12.dp, 12.dp, 12.dp)
    layoutParams = lp
    load(R.drawable.ic_comment_normal)
    this@ItemDiary.addView(this)
  }

  //评论人数
  val replyNum = TextView(context).apply {
    val lp = LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT,
      ViewGroup.LayoutParams.WRAP_CONTENT
    )
    lp.setMargins(12.dp, 12.dp, 12.dp, 12.dp)
    layoutParams = lp
    text = "0"
    this@ItemDiary.addView(this)
  }


  //测量View的大小
  override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    avatar.autoMeasure()
    author.autoMeasure()
    pushType.autoMeasure()
    timeView.autoMeasure()
    title.autoMeasure()
    info.autoMeasure()
    btnLike.autoMeasure()
    likeNum.autoMeasure()
    btnReply.autoMeasure()
    replyNum.autoMeasure()
    val wrapContentHeight =
      author.measureHeightWithMargins + pushType.measureHeightWithMargins
    + paddingTop + paddingBottom + title.measureHeightWithMargins
    + info.measureHeightWithMargins + btnLike.measureHeightWithMargins
    setMeasuredDimension(measuredWidth, wrapContentHeight)
  }


  //进行放置
  override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
    avatar.layout(0 + paddingLeft, 0 + paddingTop)
    author.let { it.layout(x = avatar.right + it.marginLeft, y = avatar.top + it.marginTop) }
    pushType.let { it.layout(x = avatar.right + it.marginLeft, y = avatar.bottom - it.height) }
    timeView.let {
      it.layout(
        x = avatar.right + it.marginLeft,
        y = avatar.bottom - it.height,
        fromRight = true
      )
    }
    title.let { it.layout(x = avatar.left, y = avatar.bottom + it.marginTop) }
    info.let { it.layout(x = avatar.left, y = title.bottom + it.marginTop) }
    likeNum.let {
      it.layout(
        x = info.measuredWidth / 2 - it.marginRight,
        info.bottom + it.marginTop
      )
    }
    btnLike.let { it.layout(x = likeNum.left - it.marginLeft, y = info.bottom + it.marginTop) }
    btnReply.let {
      it.layout(
        x = info.measuredWidth + it.marginLeft,
        y = info.bottom + it.marginTop
      )
    }
    replyNum.let {
      it.layout(
        x = btnReply.right + it.marginLeft,
        y = info.bottom + it.marginTop
      )
    }


  }
}